package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.exception.UpdateFlagException;
import com.itany.pojo.Book;
import com.itany.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Author：sl
 * Date：2019-09-21 下午5:10
 * Description：<描述>
 */
@RequestMapping("/book")
@Controller
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam(defaultValue="1")Integer page,
                                      @RequestParam(defaultValue="5")Integer rows){
        Map<String,Object> map = new HashMap<String, Object>();
        PageInfo<Book> info = bookService.findAllBook(page, rows);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        System.out.println(info.getList().get(0).getSupplierList().get(0));
        System.out.println(info.getTotal());
        return map;
    }

    @RequestMapping("modifyBookFlag")
    public ModelAndView modifyBookFlag(Book book){

        ModelAndView mv=new ModelAndView();
        try {
            bookService.modifyBookFlag(book);
        } catch (UpdateFlagException e) {
            mv.addObject("msg",e.getMessage());
        }
        mv.setViewName("/backend/booklist");
        return mv;
    }


}
